var express = require("express");
var router = express.Router();
const db = require("../db/index.js");
router.get('/list',function(req,res,next){
    console.log(req.query);
    // 获取参数   当前页码   当前每页数据量
    // 获取参数   当前页码   当前每页数据量
    let pageNumber = req.query.pageNumber
    // let pageSize = parseInt(req.body.pageSize)
    let pagesize = req.query.pagesize - 0
   // 求得sql语句中的参数
   console.log(pageNumber,pagesize);
  //  if(!pageNumber) pageNumber=1
  //  if(!pagesize) pagesize=6
   console.log(pageNumber,pagesize);
    let start = (pageNumber - 1) * pagesize
    // sql语句
    let sql = "select * from practice limit ?, ?"
    let sqlParam = [start, pagesize]
    let sqlCount = "select count(*) from practice"
  
    var count = 0
    // 执行查询
   db.query(sqlCount, null, (err, result) => {
     // 得到总记录数
     count = result[0]["count(*)"]
    
   })
  
   db.query(sql, sqlParam, (err, result) => {
     if (err) {
       res.send({
         code:0,
         message: err.message,
         ok:true
       })
     } else {
       res.send({
         code:200,
         message: "请求成功",
         ok: true,
         totalCount: count,
         data:result,
         
       })
     }
   })
     
  })
// 删除练车信息
router.post('/deletePractice',function(req,res,next){
  let id = req.query.id
  console.log(id);
  const sql = 'delete from practice where id=?'
          db.query(sql,id,function(err,result){
              if(err) {res.send({code:0,message:err.message})} 
              res.send({code:200,message:'删除成功！'})
          })
})
router.post('/updatePractice',function(req,res,next){
  let info =req.body
  const sql = `update practice set time=?,adress=? where id=? `
  db.query(sql,[info.time,info.adress,info.id],function(err,result){
    if(err){return res.send({code:0,message:err.message})} else{
      return  res.send({code:200,message:'修改成功!'})
    }
    
  })
})
// 获取练车详情
router.get('/getInfo',function(req,res){
  console.log(req.query);
  let id = req.query.id
  const sql = `select * from practice where id=?`
  db.query(sql,id,function(err,results){
      if(err){return res.send({code:0,message:err.message})} else{return res.send({code:200,message:'查询成功',data:results[0]})}
     
  })
})
router.post("/cancelPractice", function (req, res, next) {
  let id = req.query.id;
  const sql = "delete from practice where id=?";
  db.query(sql, id, function (err, result) {
    if (err) {
      return res.send({ code: 0, message: err.message });
    } else {
      res.send({ code: 200, message: "取消成功！" });
    }
  });
});


module.exports = router